﻿namespace Leetcode.N0005;
public class Solution
{
    public string LongestPalindrome(string s)
    {
        if (string.IsNullOrEmpty(s))
        {
            return "";
        }

        int longestLength = 1;
        int longestFrom = 0;
        for (int from = 0; from <= (s.Length - longestLength); from++)
        {
            for (int to = from + longestLength; to < s.Length; to++)
            {
                if (this.IsPalindrome(s, from, to))
                {
                    longestLength = to - from + 1;
                    longestFrom = from;
                }
            }

        }
        return s.Substring(longestFrom, longestLength);
    }

    private bool IsPalindrome(string s, int from, int to)
    {
        var m = from + (to - from + 1) / 2;
        for (int i = from; i < m; i++)
        {
            if (s[i] != s[to - (i - from)])
            {
                return false;
            }
        }
        return true;
    }
}
